package com.megvii.corelib.components;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.LinearLayout;

import com.megvii.corelib.R;
import com.megvii.corelib.components.wheel.StrericWheelAdapter;
import com.megvii.corelib.components.wheel.WheelView;

import java.util.Calendar;

/**
 * Created by linyue on 15/12/29.
 */
public class DateTimePickerView extends LinearLayout
{
    public static final int DEFAULT_START_YEAR = 2013;

    public static final int DEFAULT_END_YEAR = 2030;

    protected String[] yearContent = null;

    protected String[] monthContent = null;

    protected String[] dayContent = null;

    protected String[] hourContent = null;

    protected String[] minuteContent = null;

    protected String[] secondContent = null;

    protected WheelView yearWheel, monthWheel, dayWheel, hourWheel, minuteWheel, secondWheel;

    protected int startYear = DEFAULT_START_YEAR;

    protected int endYear = DEFAULT_END_YEAR;

    public DateTimePickerView(Context context)
    {
        super(context);
        init(context, null);
    }

    public DateTimePickerView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context, attrs);
    }

    public DateTimePickerView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs)
    {
        inflate(context, R.layout.time_picker, this);
        yearWheel = (WheelView) findViewById(R.id.yearwheel);
        monthWheel = (WheelView) findViewById(R.id.monthwheel);
        dayWheel = (WheelView) findViewById(R.id.daywheel);
        hourWheel = (WheelView) findViewById(R.id.hourwheel);
        minuteWheel = (WheelView) findViewById(R.id.minutewheel);
        secondWheel = (WheelView) findViewById(R.id.secondwheel);

        //TODO 在xml里可以配置startYear和endYear

        yearContent = new String[endYear - startYear + 1];
        for (int i = 0; i < yearContent.length; i++)
        {
            yearContent[i] = String.valueOf(i + startYear);
        }

        monthContent = new String[12];
        for (int i = 0; i < 12; i++)
        {
            monthContent[i] = String.valueOf(i + 1);
            if (monthContent[i].length() < 2)
            {
                monthContent[i] = "0" + monthContent[i];
            }
        }

        dayContent = new String[31];
        for (int i = 0; i < 31; i++)
        {
            dayContent[i] = String.valueOf(i + 1);
            if (dayContent[i].length() < 2)
            {
                dayContent[i] = "0" + dayContent[i];
            }
        }
        hourContent = new String[24];
        for (int i = 0; i < 24; i++)
        {
            hourContent[i] = String.valueOf(i);
            if (hourContent[i].length() < 2)
            {
                hourContent[i] = "0" + hourContent[i];
            }
        }

        minuteContent = new String[60];
        for (int i = 0; i < 60; i++)
        {
            minuteContent[i] = String.valueOf(i);
            if (minuteContent[i].length() < 2)
            {
                minuteContent[i] = "0" + minuteContent[i];
            }
        }
        secondContent = new String[60];
        for (int i = 0; i < 60; i++)
        {
            secondContent[i] = String.valueOf(i);
            if (secondContent[i].length() < 2)
            {
                secondContent[i] = "0" + secondContent[i];
            }
        }
    }

    public void setDateTime(long timestamp)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp); // 设置当前日期

        int curYear = calendar.get(Calendar.YEAR);
        int curMonth = calendar.get(Calendar.MONTH) + 1;
        int curDay = calendar.get(Calendar.DAY_OF_MONTH);
        int curHour = calendar.get(Calendar.HOUR_OF_DAY);
        int curMinute = calendar.get(Calendar.MINUTE);
        int curSecond = calendar.get(Calendar.SECOND);

        yearWheel.setAdapter(new StrericWheelAdapter(yearContent));
        int yearIndex = curYear - startYear;
        if (yearIndex < 0)
        {
            yearIndex = 0;
        }
        else if (yearIndex >= yearContent.length)
        {
            yearIndex = yearContent.length - 1;
        }
        yearWheel.setCurrentItem(yearIndex);
        yearWheel.setCyclic(true);
        yearWheel.setInterpolator(new AnticipateOvershootInterpolator());

        monthWheel.setAdapter(new StrericWheelAdapter(monthContent));

        monthWheel.setCurrentItem(curMonth - 1);

        monthWheel.setCyclic(true);
        monthWheel.setInterpolator(new AnticipateOvershootInterpolator());

        dayWheel.setAdapter(new StrericWheelAdapter(dayContent));
        dayWheel.setCurrentItem(curDay - 1);
        dayWheel.setCyclic(true);
        dayWheel.setInterpolator(new AnticipateOvershootInterpolator());

        hourWheel.setAdapter(new StrericWheelAdapter(hourContent));
        hourWheel.setCurrentItem(curHour);
        hourWheel.setCyclic(true);
        hourWheel.setInterpolator(new AnticipateOvershootInterpolator());

        minuteWheel.setAdapter(new StrericWheelAdapter(minuteContent));
        minuteWheel.setCurrentItem(curMinute);
        minuteWheel.setCyclic(true);
        minuteWheel.setInterpolator(new AnticipateOvershootInterpolator());

        secondWheel.setAdapter(new StrericWheelAdapter(secondContent));
        secondWheel.setCurrentItem(curSecond);
        secondWheel.setCyclic(true);
        secondWheel.setInterpolator(new AnticipateOvershootInterpolator());
    }

    public long getTimetamp()
    {
        int year = Integer.valueOf(yearWheel.getCurrentItemValue());
        int month = Integer.valueOf(monthWheel.getCurrentItemValue()) - 1;
        int day = Integer.valueOf(dayWheel.getCurrentItemValue());
        int hour = Integer.valueOf(hourWheel.getCurrentItemValue());
        int minute = Integer.valueOf(minuteWheel.getCurrentItemValue());
        int second = Integer.valueOf(secondWheel.getCurrentItemValue());
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day, hour, minute, second);
        return calendar.getTimeInMillis();
    }
}
